﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.Customer
{
    public partial class IndexImgManage : BasePage
    {
        ExcuteDao dao = new ExcuteDao();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                showImage();
            }
        }
        private void showImage()
        {
            HyperLink1.Visible = false;
            HyperLink2.Visible = false;
            HyperLink3.Visible = false;
            HyperLink4.Visible = false;
            HyperLink5.Visible = false;
            Button1.Visible = false;
            Button2.Visible = false;
            Button3.Visible = false;
            Button4.Visible = false;
            Button5.Visible = false;
            DataTable dt1 = dao.GET_IndexImage(1);
            if (dt1 != null && dt1.Rows.Count > 0)
            {
                if (dt1.Rows[0]["Image_Isdel"].ToString().Equals("False"))
                {
                    Button1.Visible = true;
                    HyperLink1.Visible = true;
                }
                HyperLink1.NavigateUrl = "ShowIndexImage.aspx?Image_ID=" + dt1.Rows[0]["Image_ID"].ToString();
                HyperLink1.Text = dt1.Rows[0]["Image_Name"].ToString();
                HyperLink1.ToolTip = dt1.Rows[0]["Image_Name"].ToString();
                HiddenField1.Value = dt1.Rows[0]["Image_ID"].ToString();
                
            }
            DataTable dt2 = dao.GET_IndexImage(2);
            if (dt2 != null && dt2.Rows.Count > 0)
            {
                if (dt2.Rows[0]["Image_Isdel"].ToString().Equals("False"))
                {
                    Button2.Visible = true;
                    HyperLink2.Visible = true;
                }
                HyperLink2.NavigateUrl = "ShowIndexImage.aspx?Image_ID=" + dt2.Rows[0]["Image_ID"].ToString();
                HyperLink2.Text = dt2.Rows[0]["Image_Name"].ToString();
                HyperLink2.ToolTip = dt2.Rows[0]["Image_Name"].ToString();
                HiddenField2.Value = dt2.Rows[0]["Image_ID"].ToString();
            }
            DataTable dt3 = dao.GET_IndexImage(3);
            if (dt3 != null && dt3.Rows.Count > 0)
            {
                if (dt3.Rows[0]["Image_Isdel"].ToString().Equals("False"))
                {
                    Button3.Visible = true;
                    HyperLink3.Visible = true;
                }
                HyperLink3.NavigateUrl = "ShowIndexImage.aspx?Image_ID=" + dt3.Rows[0]["Image_ID"].ToString();
                HyperLink3.Text = dt3.Rows[0]["Image_Name"].ToString();
                HyperLink3.ToolTip = dt3.Rows[0]["Image_Name"].ToString();
                HiddenField3.Value = dt3.Rows[0]["Image_ID"].ToString();
            }
            DataTable dt4 = dao.GET_IndexImage(4);
            if (dt4 != null && dt4.Rows.Count > 0)
            {
                if (dt4.Rows[0]["Image_Isdel"].ToString().Equals("False"))
                {
                    Button4.Visible = true;
                    HyperLink4.Visible = true;
                }
                HyperLink4.NavigateUrl = "ShowIndexImage.aspx?Image_ID=" + dt4.Rows[0]["Image_ID"].ToString();
                HyperLink4.Text = dt4.Rows[0]["Image_Name"].ToString();
                HyperLink4.ToolTip = dt4.Rows[0]["Image_Name"].ToString();
                HiddenField4.Value = dt4.Rows[0]["Image_ID"].ToString();
            }
            DataTable dt5 = dao.GET_IndexImage(5);
            if (dt5 != null && dt5.Rows.Count > 0)
            {
                if (dt5.Rows[0]["Image_Isdel"].ToString().Equals("False"))
                {
                    Button5.Visible = true;
                    HyperLink5.Visible = true;
                }
                HyperLink5.NavigateUrl = "ShowIndexImage.aspx?Image_ID=" + dt5.Rows[0]["Image_ID"].ToString();
                HyperLink5.Text = dt5.Rows[0]["Image_Name"].ToString();
                HyperLink5.ToolTip = dt5.Rows[0]["Image_Name"].ToString();
                HiddenField5.Value = dt5.Rows[0]["Image_ID"].ToString();
            }
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int isok = 0;
            if (FileUpload1.FileName.Length > 0)
            {
                if (IsImage(FileUpload1))
                {
                    if (HiddenField1.Value.Length > 0)
                    {
                        isok = dao.Update_IndexImage(Convert.ToInt32(HiddenField1.Value),
                              "1" + System.IO.Path.GetExtension(FileUpload1.FileName).ToString(),
                              FileUpload1.FileBytes,
                              FileUpload1.PostedFile.ContentType,
                              getCurrentUser().UserID);
                    }
                    else
                    {
                        isok = dao.Insert_IndexImage("1" + System.IO.Path.GetExtension(FileUpload1.FileName).ToString(),
                             FileUpload1.FileBytes,
                             FileUpload1.PostedFile.ContentType,
                             getCurrentUser().UserID);
                    }
                }
            }
            if (FileUpload2.FileName.Length > 0)
            {
                if (IsImage(FileUpload2))
                {
                    if (HiddenField2.Value.Length > 0)
                    {
                        isok = dao.Update_IndexImage(Convert.ToInt32(HiddenField2.Value),
                            "2" + System.IO.Path.GetExtension(FileUpload2.FileName).ToString(),
                            FileUpload2.FileBytes,
                            FileUpload2.PostedFile.ContentType,
                            getCurrentUser().UserID);

                    }
                    else
                    {
                        isok = dao.Insert_IndexImage("2" + System.IO.Path.GetExtension(FileUpload2.FileName).ToString(),
                            FileUpload2.FileBytes,
                            FileUpload2.PostedFile.ContentType,
                            getCurrentUser().UserID);
                    }
                }
            }
            if (FileUpload3.FileName.Length > 0)
            {
                if (IsImage(FileUpload3))
                {
                    if (HiddenField3.Value.Length > 0)
                    {
                        isok = dao.Update_IndexImage(Convert.ToInt32(HiddenField3.Value),
                            "3" + System.IO.Path.GetExtension(FileUpload3.FileName).ToString(),
                            FileUpload3.FileBytes,
                            FileUpload3.PostedFile.ContentType,
                            getCurrentUser().UserID);

                    }
                    else
                    {
                        isok = dao.Insert_IndexImage("3" + System.IO.Path.GetExtension(FileUpload3.FileName).ToString(),
                            FileUpload3.FileBytes,
                            FileUpload3.PostedFile.ContentType,
                            getCurrentUser().UserID);
                    }
                }
            }
            if (FileUpload4.FileName.Length > 0)
            {
                if (IsImage(FileUpload4))
                {
                    if (HiddenField4.Value.Length > 0)
                    {
                        isok = dao.Update_IndexImage(Convert.ToInt32(HiddenField4.Value),
                            "4" + System.IO.Path.GetExtension(FileUpload4.FileName).ToString(),
                            FileUpload4.FileBytes,
                            FileUpload4.PostedFile.ContentType,
                            getCurrentUser().UserID);
                    }
                    else
                    {
                        isok = dao.Insert_IndexImage("4" + System.IO.Path.GetExtension(FileUpload4.FileName).ToString(),
                            FileUpload4.FileBytes,
                            FileUpload4.PostedFile.ContentType,
                            getCurrentUser().UserID);
                    }
                }
            }
            if (FileUpload5.FileName.Length > 0)
            {
                if (IsImage(FileUpload5))
                {
                    if (HiddenField5.Value.Length > 0)
                    {
                        isok = dao.Update_IndexImage(Convert.ToInt32(HiddenField5.Value),
                           "5" + System.IO.Path.GetExtension(FileUpload5.FileName).ToString(),
                            FileUpload5.FileBytes,
                            FileUpload5.PostedFile.ContentType,
                            getCurrentUser().UserID);
                    }
                    else
                    {
                        isok = dao.Insert_IndexImage("5" + System.IO.Path.GetExtension(FileUpload5.FileName).ToString(),
                            FileUpload5.FileBytes,
                            FileUpload5.PostedFile.ContentType,
                            getCurrentUser().UserID);
                    }
                }
            }
            if(isok>0)
            {
                showImage();
                ShowMsg("上传成功", 0);
            }
            else
            {
                ShowMsg("无上传文件或上传失败，请确认！", 1);
            }
        }
        private bool IsImage(FileUpload fileupload)
        {
            string fileleixing = System.IO.Path.GetExtension(fileupload.FileName).ToString(); //获取文件的扩展名
            if (fileleixing == ".gif" || fileleixing == ".jpg" || fileleixing == ".bmp")
            {
                int fileLenth = fileupload.PostedFile.ContentLength / 1024;
                if (fileLenth > 2048)
                {
                    ShowMsg("文件大小不能超过2MB", 1);
                    return false;
                }
            }
            else
            {
                ShowMsg("文件类型不一致", 1);
                return false;
            }
            return true;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {

            int rtn = dao.Delete_IndexImage(Convert.ToInt32(HiddenField1.Value));
            if (rtn > 0)
            {
                ShowMsg("删除成功", 0);
                showImage();
            }
            else
            {
                ShowMsg("删除失败", 1);
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int rtn = dao.Delete_IndexImage(Convert.ToInt32(HiddenField2.Value));
            if (rtn > 0)
            {
                ShowMsg("删除成功", 0);
                showImage();
            }
            else
            {
                ShowMsg("删除失败", 1);
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            int rtn = dao.Delete_IndexImage(Convert.ToInt32(HiddenField3.Value));
            if (rtn > 0)
            {
                ShowMsg("删除成功", 0);
                showImage();
            }
            else
            {
                ShowMsg("删除失败", 1);
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            int rtn = dao.Delete_IndexImage(Convert.ToInt32(HiddenField4.Value));
            if (rtn > 0)
            {
                ShowMsg("删除成功", 0);
                showImage();
            }
            else
            {
                ShowMsg("删除失败", 1);
            }
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            int rtn = dao.Delete_IndexImage(Convert.ToInt32(HiddenField5.Value));
            if (rtn > 0)
            {
                ShowMsg("删除成功", 0);
                showImage();
            }
            else
            {
                ShowMsg("删除失败", 1);
            }
        }
    }
}